package com.edu.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Author Yang
 * @Description 数据源配置
 * @Date 16:04 2021/5/6
 **/
@Configuration
public class DataSourceConfig {

    @Value("${jdbc.url:jdbc:mysql://127.0.0.1:3306/low_code?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT}")
    private String url;

    @Value("${jdbc.username:root}")
    private String username;

    @Value("${jdbc.password:root}")
    private String password;

    @Value("${jdbc.maxActive:8}")
    private Integer maxActive;

    @Value("${jdbc.maxIdle:2}")
    private Integer maxIdle;

    @Value("${jdbc.defaultAutoCommit:false}")
    private Boolean defaultAutoCommit;

    @Value("${jdbc.timeBetweenEvictionRunsMillis:30000}")
    private Integer timeBetweenEvictionRunsMillis;

    @Value("${jdbc.minEvictableIdleTimeMillis:600000}")
    private Integer minEvictableIdleTimeMillis;

    @Value("${jdbc.driver:com.mysql.cj.jdbc.Driver}")
    private String driver;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driver);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(maxIdle);
        dataSource.setDefaultAutoCommit(defaultAutoCommit);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        return dataSource;
    }
}
